/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cjava.system.bean.meeting;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.view.ViewScoped;
import org.cjava.system.model.Department;
import org.cjava.system.model.District;
import org.cjava.system.model.Meeting;
import org.cjava.system.model.MeetingPerson;
import org.cjava.system.model.Person;
import org.cjava.system.model.Province;
import org.cjava.system.model.University;
import org.cjava.system.service.interfac.IDepartmentService;
import org.cjava.system.service.interfac.IDistrictService;
import org.cjava.system.service.interfac.IMeetingPersonService;
import org.cjava.system.service.interfac.IMeetingService;
import org.cjava.system.service.interfac.IPersonService;
import org.cjava.system.service.interfac.IProvinceService;
import org.cjava.system.service.interfac.IUniversityService;
import org.cjava.system.util.Clazz;
import org.cjava.system.util.faces.FacesUtil;

/**
 *
 * @author max
 */
@ManagedBean
@SessionScoped
public class PersonMeetingBean implements Serializable {
private static final long serialVersionUID = 1L;
    @ManagedProperty("#{meetingService}")
    IMeetingService meetingService;
    @ManagedProperty("#{personService}")
    IPersonService personService;
    @ManagedProperty("#{meetingPersonService}")
    IMeetingPersonService meetingPersonService;
    @ManagedProperty("#{departmentService}")
    IDepartmentService departmentService;
    @ManagedProperty("#{provinceService}")
    IProvinceService provinceService;
    @ManagedProperty("#{districtService}")
    IDistrictService districtService;
    @ManagedProperty("#{universityService}")
    IUniversityService universityService;
    @ManagedProperty("#{clazz}")
    Clazz clazz;

    //////////////////////////////////////////
    private String dni;
    private List<Meeting> listMeeting;
    private Meeting selectedMeeting;
    private Person person = new Person();
    /////////////////////////////////////////
    private List<Department> listDepartment;
    private List<Province> listProvince = new ArrayList<Province>();
    private List<District> listDistrict = new ArrayList<District>();
    private List<University> listUniversity;

    private Integer idDepartment;
    private Integer idProvince;
    private Integer idDistrict;
    /////////////////////////////////////////
    private boolean noOcultarBusqueda;
    private boolean mostrarFormulario;

    public void init() throws Exception {
        mostrarFormulario = false;
        if (getListMeeting().isEmpty()) {
            noOcultarBusqueda = false;
        } else {
            noOcultarBusqueda = true;
        }

        System.out.println("Ingreso al contructor");
    }

    public void actualizar() throws Exception {
        System.out.println("Ingresando a actualizar");
        listMeeting = null;
        getListMeeting();
    }

    public void searchPersonByDocumentDNI() throws Exception {
        listMeeting = null;
        if (!getListMeeting().isEmpty()) {
            System.out.println("Buscando por DNI.");
            System.out.println(dni);
            List<Person> persons = personService.listHQL("from Person p where p.dni='" + dni + "'");
            if (persons.isEmpty()) {
                //SE TIENE QUE REGISTRAR
                System.out.println("No existe usuario, asi que debe de registrarse para el evento.");
                mostrarFormulario = true;
                person = new Person();
                FacesUtil.getRequestContext().update("sectionFormId");

            } else {
                //DEBE PREGUNTAR SI ES QUE YA ESTA REGISTRADO PARA ESE EVENTO
                System.out.println("Ya existe un seguidor asi que verifica si ya se encuentra registrado en el evento");
                Person person = persons.get(0);
                String HQLMet = "from MeetingPerson m where m.idPerson.id=" + person.getId()
                        + " and m.idMeeting.id=" + selectedMeeting.getId();
                List<MeetingPerson> listMeetingPerson = meetingPersonService.listHQL(HQLMet);
                System.out.println(listMeetingPerson);
                if (listMeetingPerson.isEmpty()) {
                    //REGISTRAR
                    MeetingPerson meetingPerson = new MeetingPerson();
                    meetingPerson.setDateRegister(new Date());
                    meetingPerson.setIdMeeting(selectedMeeting);
                    meetingPerson.setIdPerson(person);
                    meetingPersonService.save(meetingPerson);

                    noOcultarBusqueda = true;
                    mostrarFormulario = false;

                    FacesUtil.addMessage("Se registro con éxito...", "Sr(a). " + person.getName()
                            + " lo esperamos.", FacesMessage.SEVERITY_INFO);
                    FacesUtil.getRequestContext().update("globalMessage");
                } else {
                    noOcultarBusqueda = true;
                    mostrarFormulario = false;
                    FacesUtil.addMessage("Ud. Ya se encuentra registrado para este evento", null, FacesMessage.SEVERITY_INFO);
                    FacesUtil.getRequestContext().update("globalMessage");
                }
                this.person = new Person();
            }
        }
    }

    public void savePersonMeeting() throws Exception {
        person.setDni(dni);
        person.setStartDate(new Date());
        for (int i = 0; i < listDistrict.size(); i++) {
            if (listDistrict.get(i).getId().equals(idDistrict)) {
                person.setIdDistrict(listDistrict.get(i));
                break;
            }
        }
        personService.save(person);
        //REGISTRANDO AL EVENTO A LA PERSONA QUE SE ESTA REGISTRANDO
        MeetingPerson meetingPerson = new MeetingPerson();
        meetingPerson.setDateRegister(new Date());
        meetingPerson.setIdMeeting(selectedMeeting);
        meetingPerson.setIdPerson(person);
        meetingPersonService.save(meetingPerson);
        //////////////////////////////////////
        person = new Person();
        dni = null;
        noOcultarBusqueda = true;
        mostrarFormulario = false;
        FacesUtil.addMessage("Registrado con éxito.", "Ya puede asistir al evento", FacesMessage.SEVERITY_INFO);
        FacesUtil.getRequestContext().update("globalMessage");
    }

    public void updateProvince() throws Exception {
        listProvince = provinceService.listHQL("from Province p where p.idDepartment.id=" + idDepartment);
    }

    public void updateDistrict() throws Exception {
        listDistrict = districtService.listHQL("from District d where d.idProvince.id=" + idProvince);
    }

    public String getDni() {
        return dni;
    }

    public void setDni(String dni) {
        this.dni = dni;
    }

    public List<Meeting> getListMeeting() throws Exception {
        if (listMeeting == null) {
            String HQL = "from Meeting m where m.idState.id="
                    + clazz.STATE(Clazz._STATE._NAME._MEETING_SOLO_SOCIOS, Clazz._STATE._ENTITY._Meeting).getId();
            listMeeting = meetingService.listHQL(HQL);
            if (listMeeting.isEmpty()) {
                noOcultarBusqueda = false;
            } else {
                noOcultarBusqueda = true;
            }

        }
        return listMeeting;
    }

    public List<University> getListUniversity() throws Exception {
        if (listUniversity == null) {
            listUniversity = universityService.list();
        }
        return listUniversity;
    }

    public void setListUniversity(List<University> listUniversity) {
        this.listUniversity = listUniversity;
    }

    public void setListMeeting(List<Meeting> listMeeting) {
        this.listMeeting = listMeeting;
    }

    public Meeting getSelectedMeeting() {
        return selectedMeeting;
    }

    public Integer getIdDistrict() {
        return idDistrict;
    }

    public void setIdDistrict(Integer idDistrict) {
        this.idDistrict = idDistrict;
    }

    public boolean isNoOcultarBusqueda() {
        return noOcultarBusqueda;
    }

    public boolean isMostrarFormulario() {
        return mostrarFormulario;
    }

    public Integer getIdDepartment() {
        return idDepartment;
    }

    public void setIdDepartment(Integer idDepartment) {
        this.idDepartment = idDepartment;
    }

    public Integer getIdProvince() {
        return idProvince;
    }

    public void setIdProvince(Integer idProvince) {
        this.idProvince = idProvince;
    }

    public void setSelectedMeeting(Meeting selectedMeeting) {
        this.selectedMeeting = selectedMeeting;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<Department> getListDepartment() throws Exception {
        if (listDepartment == null) {
            listDepartment = departmentService.list();
        }
        return listDepartment;
    }

    public void setListDepartment(List<Department> listDepartment) {
        this.listDepartment = listDepartment;
    }

    public List<Province> getListProvince() {
        return listProvince;
    }

    public void setListProvince(List<Province> listProvince) {
        this.listProvince = listProvince;
    }

    public List<District> getListDistrict() {
        return listDistrict;
    }

    public void setListDistrict(List<District> listDistrict) {
        this.listDistrict = listDistrict;
    }

    ////////////
    public void setMeetingService(IMeetingService meetingService) {
        this.meetingService = meetingService;
    }

    public void setPersonService(IPersonService personService) {
        this.personService = personService;
    }

    public void setUniversityService(IUniversityService universityService) {
        this.universityService = universityService;
    }

    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    public void setProvinceService(IProvinceService provinceService) {
        this.provinceService = provinceService;
    }

    public void setDistrictService(IDistrictService districtService) {
        this.districtService = districtService;
    }

    public void setMeetingPersonService(IMeetingPersonService meetingPersonService) {
        this.meetingPersonService = meetingPersonService;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }
    //////////////

}
